{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>Conpany</th>\n",
       "      <th>Name</th>\n",
       "      <th>Sale2013</th>\n",
       "      <th>Sale2014</th>\n",
       "      <th>Sale2015</th>\n",
       "      <th>Sale2016</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>Apple</td>\n",
       "      <td>苹果</td>\n",
       "      <td>5000</td>\n",
       "      <td>5050</td>\n",
       "      <td>5050</td>\n",
       "      <td>5050</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>Google</td>\n",
       "      <td>谷歌</td>\n",
       "      <td>3500</td>\n",
       "      <td>3800</td>\n",
       "      <td>3800</td>\n",
       "      <td>3800</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>Facebook</td>\n",
       "      <td>脸书</td>\n",
       "      <td>2300</td>\n",
       "      <td>2900</td>\n",
       "      <td>2900</td>\n",
       "      <td>2900</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>Amozon</td>\n",
       "      <td>亚马逊</td>\n",
       "      <td>2100</td>\n",
       "      <td>2500</td>\n",
       "      <td>2500</td>\n",
       "      <td>2500</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>Tencent</td>\n",
       "      <td>腾讯</td>\n",
       "      <td>3100</td>\n",
       "      <td>3300</td>\n",
       "      <td>3300</td>\n",
       "      <td>3300</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "    Conpany Name  Sale2013  Sale2014  Sale2015  Sale2016\n",
       "0     Apple   苹果      5000      5050      5050      5050\n",
       "1    Google   谷歌      3500      3800      3800      3800\n",
       "2  Facebook   脸书      2300      2900      2900      2900\n",
       "3    Amozon  亚马逊      2100      2500      2500      2500\n",
       "4   Tencent   腾讯      3100      3300      3300      3300"
      ]
     },
     "execution_count": 1,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "import pandas as pd\n",
    "import numpy as py\n",
    "\n",
    "mydata=pd.DataFrame({\n",
    "\"Name\":[\"苹果\",\"谷歌\",\"脸书\",\"亚马逊\",\"腾讯\"],\n",
    "\"Conpany\":[\"Apple\",\"Google\",\"Facebook\",\"Amozon\",\"Tencent\"],\n",
    "\"Sale2013\":[5000,3500,2300,2100,3100],\n",
    "\"Sale2014\":[5050,3800,2900,2500,3300],\n",
    "\"Sale2015\":[5050,3800,2900,2500,3300],\n",
    "\"Sale2016\":[5050,3800,2900,2500,3300]\n",
    "       })\n",
    "mydata"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>Name</th>\n",
       "      <th>Conpany</th>\n",
       "      <th>Year</th>\n",
       "      <th>Sale</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>苹果</td>\n",
       "      <td>Apple</td>\n",
       "      <td>Sale2013</td>\n",
       "      <td>5000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>谷歌</td>\n",
       "      <td>Google</td>\n",
       "      <td>Sale2013</td>\n",
       "      <td>3500</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>脸书</td>\n",
       "      <td>Facebook</td>\n",
       "      <td>Sale2013</td>\n",
       "      <td>2300</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>亚马逊</td>\n",
       "      <td>Amozon</td>\n",
       "      <td>Sale2013</td>\n",
       "      <td>2100</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>腾讯</td>\n",
       "      <td>Tencent</td>\n",
       "      <td>Sale2013</td>\n",
       "      <td>3100</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>5</th>\n",
       "      <td>苹果</td>\n",
       "      <td>Apple</td>\n",
       "      <td>Sale2014</td>\n",
       "      <td>5050</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>6</th>\n",
       "      <td>谷歌</td>\n",
       "      <td>Google</td>\n",
       "      <td>Sale2014</td>\n",
       "      <td>3800</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>7</th>\n",
       "      <td>脸书</td>\n",
       "      <td>Facebook</td>\n",
       "      <td>Sale2014</td>\n",
       "      <td>2900</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>8</th>\n",
       "      <td>亚马逊</td>\n",
       "      <td>Amozon</td>\n",
       "      <td>Sale2014</td>\n",
       "      <td>2500</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>9</th>\n",
       "      <td>腾讯</td>\n",
       "      <td>Tencent</td>\n",
       "      <td>Sale2014</td>\n",
       "      <td>3300</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>10</th>\n",
       "      <td>苹果</td>\n",
       "      <td>Apple</td>\n",
       "      <td>Sale2015</td>\n",
       "      <td>5050</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>11</th>\n",
       "      <td>谷歌</td>\n",
       "      <td>Google</td>\n",
       "      <td>Sale2015</td>\n",
       "      <td>3800</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>12</th>\n",
       "      <td>脸书</td>\n",
       "      <td>Facebook</td>\n",
       "      <td>Sale2015</td>\n",
       "      <td>2900</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>13</th>\n",
       "      <td>亚马逊</td>\n",
       "      <td>Amozon</td>\n",
       "      <td>Sale2015</td>\n",
       "      <td>2500</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>14</th>\n",
       "      <td>腾讯</td>\n",
       "      <td>Tencent</td>\n",
       "      <td>Sale2015</td>\n",
       "      <td>3300</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>15</th>\n",
       "      <td>苹果</td>\n",
       "      <td>Apple</td>\n",
       "      <td>Sale2016</td>\n",
       "      <td>5050</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>16</th>\n",
       "      <td>谷歌</td>\n",
       "      <td>Google</td>\n",
       "      <td>Sale2016</td>\n",
       "      <td>3800</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>17</th>\n",
       "      <td>脸书</td>\n",
       "      <td>Facebook</td>\n",
       "      <td>Sale2016</td>\n",
       "      <td>2900</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>18</th>\n",
       "      <td>亚马逊</td>\n",
       "      <td>Amozon</td>\n",
       "      <td>Sale2016</td>\n",
       "      <td>2500</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>19</th>\n",
       "      <td>腾讯</td>\n",
       "      <td>Tencent</td>\n",
       "      <td>Sale2016</td>\n",
       "      <td>3300</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "   Name   Conpany      Year  Sale\n",
       "0    苹果     Apple  Sale2013  5000\n",
       "1    谷歌    Google  Sale2013  3500\n",
       "2    脸书  Facebook  Sale2013  2300\n",
       "3   亚马逊    Amozon  Sale2013  2100\n",
       "4    腾讯   Tencent  Sale2013  3100\n",
       "5    苹果     Apple  Sale2014  5050\n",
       "6    谷歌    Google  Sale2014  3800\n",
       "7    脸书  Facebook  Sale2014  2900\n",
       "8   亚马逊    Amozon  Sale2014  2500\n",
       "9    腾讯   Tencent  Sale2014  3300\n",
       "10   苹果     Apple  Sale2015  5050\n",
       "11   谷歌    Google  Sale2015  3800\n",
       "12   脸书  Facebook  Sale2015  2900\n",
       "13  亚马逊    Amozon  Sale2015  2500\n",
       "14   腾讯   Tencent  Sale2015  3300\n",
       "15   苹果     Apple  Sale2016  5050\n",
       "16   谷歌    Google  Sale2016  3800\n",
       "17   脸书  Facebook  Sale2016  2900\n",
       "18  亚马逊    Amozon  Sale2016  2500\n",
       "19   腾讯   Tencent  Sale2016  3300"
      ]
     },
     "execution_count": 2,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "mydata1=mydata.melt(\n",
    "id_vars=[\"Name\",\"Conpany\"],   #要保留的主字段\n",
    "var_name=\"Year\",                     #拉长的分类变量\n",
    "value_name=\"Sale\"                  #拉长的度量值名称\n",
    "        )\n",
    "mydata1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>0</th>\n",
       "      <th>1</th>\n",
       "      <th>2</th>\n",
       "      <th>3</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>Action</td>\n",
       "      <td>Adventure</td>\n",
       "      <td>Science Fiction</td>\n",
       "      <td>Thriller</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>Adventure</td>\n",
       "      <td>Science Fiction</td>\n",
       "      <td>Thriller</td>\n",
       "      <td>None</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>Action</td>\n",
       "      <td>Crime</td>\n",
       "      <td>Thriller</td>\n",
       "      <td>None</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>others</td>\n",
       "      <td>None</td>\n",
       "      <td>None</td>\n",
       "      <td>None</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "           0                1                2         3\n",
       "0     Action        Adventure  Science Fiction  Thriller\n",
       "1  Adventure  Science Fiction         Thriller      None\n",
       "2     Action            Crime         Thriller      None\n",
       "3     others             None             None      None"
      ]
     },
     "execution_count": 9,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "mydata=pd.DataFrame({\n",
    "\"id\":[\"1\",\"2\",\"3\",\"4\"],\n",
    "\"imdb_id\":[\"2121\",\"ewrw\",\"45454\",\"gdddf\"],\n",
    "\"genres\":[\"Action|Adventure|Science Fiction|Thriller\",\"Adventure|Science Fiction|Thriller\",\"Action|Crime|Thriller\",\"others\"]\n",
    "})\n",
    "\n",
    "# mydata1=mydata.melt(\n",
    "# id_vars=[\"id\",\"imdb_id\"],   #要保留的主字段\n",
    "# var_name=\"genres\",                     #拉长的分类变量\n",
    "# value_name=\"Sale\"                  #拉长的度量值名称\n",
    "#         )\n",
    "# mydata1\n",
    "\n",
    "new_df = mydata['genres'].str.split('|', expand=True)\n",
    "new_df\n",
    "pd.DataFrame({\n",
    "    'id':mydata['id']\n",
    "})\n",
    "new_df['id'] = mydata['id']\n",
    "new_df['imdb_id'] = mydata['imdb_id']\n",
    "\n",
    "# mydata1=new_df.melt(\n",
    "# id_vars=[\"id\",\"imdb_id\"],   #要保留的主字段\n",
    "# var_name=\"aa\",                     #拉长的分类变量\n",
    "# value_name=\"genres\"                  #拉长的度量值名称\n",
    "#         )\n",
    "# mydata1.drop('aa',axis=1).dropna()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.1"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
